import ply.lex as lex
import ply.yacc as yacc

var_names = []



tokens = [
    "SYMBOL",
    "SPACE",
    "EQUAL",
    "COMMENT",
    "SINGLE_LINE_COMMENT",
    "MULTI_LINE_COMMENT",
    "END",
    "PRECOMP_MACRO",
    "OPEN_BRACE",
    "CLOSE_BRACE",
    "SEMI_COLON",
    "COLON",
    "COMMA",
    "TAB",
    "MINUS",
    "PLUS",
    "DIVIDE",
    "MULTIPLE",
    "POSITIVE_NUMBER",
    "DEFINE_SENTENCE",
    "OPEN_PAREN",
    "CLOSE_PAREN"   
]


reserved = {
   'enum' : 'ENUM',
}

tokens = tokens + list(reserved.values())

t_EQUAL = (
    r"[=]")

##t_COMMENT = (
##    r"//.*$")

t_OPEN_PAREN = r'\('
t_CLOSE_PAREN = r'\)'

t_SEMI_COLON = r';'
t_COLON = r':'
t_COMMA = r','


t_MINUS = r'\-'
t_PLUS = r'\+'
t_DIVIDE = r'/[^/]'
t_MULTIPLE = r'\*'

##t_SAPCE = (
##     r"[\s]+")

#t_END =(
#     r"[\n]+")

def t_SYMBOL(t):
    r"[a-zA-Z_][a-zA-Z0-9_]*"
    t.type = reserved.get(t.value,'SYMBOL')    # Check for reserved words
    return t

def t_OPEN_BRACE(t):
    r"{[\x20\t\n]*"
    return t

def t_CLOSE_BRACE(t):
    r"}[\x20\t\n]*"
    return t

def t_DEFINE_SENTENCE(t):
    r"\#.*[\n]*"
    pass 

def t_SINGLE_LINE_COMMENT(t):
    r"//.*\n"
    return t  

def t_MULTI_LINE_COMMENT(t):
    r"/\*([^*]|[\r\n]|(\*+([^*/]|[\r\n])))*\*+/"
    pass

def t_PRECOMP_MACRO(t):
    r"\#.*\n"
    pass

def t_SPACE(t):
     r"[\x20]+"
     pass
     
def t_TAB(t):
     r"\t"
     pass
 
def t_END(t):
     r"[\n]+"
     return t
     
def t_POSITIVE_NUMBER(t):
    r"\d+"
    t.value = int(t.value)
    return t

def t_error(t):
    raise TypeError("Unknown text '%s'" % (t.value,))

lex.lex()

class Atom(object):
    def __init__(self, symbol, count):
        self.symbol = symbol
        self.count = count
    def __repr__(self):
        return "Atom(%r, %r)" % (self.symbol, self.count)
    
class Item(object):
    def __init__(self,symbol,value,comment):
        self.symbol = symbol
        self.value = value
        str = comment
        str = str.replace('//','')
        str = str.replace('\n','')
        str = str.replace(' ','')
        str = str.replace('','')
        self.comment = str
    def __repr__(self):
        return "Item (%r, %r, %r)" % (self.symbol,self.value,self.comment)

def print_item(p,file):
     str = ("<mGameStrings dwGameStringID=\"%r\" dwGameStringIDName=\"%s\" nErrorCode=\"%r\" szShowPos=\"CMST_MESSAGEBOX;CMST_ANNOUNCEMENT\" szGameString=\"%s\">\n" \
        "\t<szShowPosArray IsStaticArray=\"0\" [XlsProp]=\"0\" ArrayCount=\"2\">\n"\
					"\t\t<szShowPosArray v=\"22\"/>\n" \
					"\t\t<szShowPosArray v=\"20\"/>\n" \
				"\t</szShowPosArray>\n</mGameStrings>\n") % (p.value,p.symbol,p.value,p.comment )
     file.write(str)

def p_programme(p):
    """
    programme : enum_block
    """
    
def p_NUMBER(p):
    """
    NUMBER : OPEN_PAREN MINUS NUMBER CLOSE_PAREN
    NUMBER : POSITIVE_NUMBER
    NUMBER : NUMBER OPERATOR NUMBER    
    """
    if(len(p)==5):
        p[0] = -1 * p[3];
    elif(len(p)==2):
        p[0] = p[1];
    else:
        if(p[2]=='*'):
            p[0] = p[1] * p[3]
        elif(p[2]=='/'):
            p[0] = p[1] / p[3]
        elif(p[2]=='+'):
            p[0] = p[1] + p[3]
        elif(p[2]=='-'):
            p[0] = p[1] - p[3]
        
        
    

def p_OPERATOR(p):
    """
    OPERATOR : MINUS
    OPERATOR : PLUS
    OPERATOR : DIVIDE
    OPERATOR : MULTIPLE
    """
    p[0] = p[1]

    
def p_multiline_SYMBOL(p):
    """
    multiline_SYMBOL : SYMBOL END
    multiline_SYMBOL : SYMBOL
    """
    p[0] = p[1]
    
def p_multi_EXPRESSION(p):
    """
    multi_EXPRESSION : multi_EXPRESSION single_EXPRESSION
    multi_EXPRESSION : single_EXPRESSION
    """
    
def p_single_EXPRESSION(p):
    """
    single_EXPRESSION : multiline_SYMBOL EQUAL NUMBER COMMA SINGLE_LINE_COMMENT
    single_EXPRESSION : multiline_SYMBOL EQUAL NUMBER COMMA
    single_EXPRESSION : single_EXPRESSION END
    """
    if(len(p)==5):
        var_names.append(Item(p[1],p[3],""))
    elif(len(p)==6):
        var_names.append(Item(p[1],p[3],p[5]))
    

def p_enum_block(p):
    """
    enum_block : ENUM multiline_SYMBOL OPEN_BRACE multi_EXPRESSION CLOSE_BRACE SEMI_COLON
    """


    
def p_error(p):
    raise TypeError("unknown text at %s " % (p))
    
yacc.yacc()


##s = "9"


f = open("test.h")


s = f.read()



##lex.input(s)
##for tok in iter(lex.token, None):
##    print (tok.type), (tok.value)

yacc.parse(s)
##for atom in yacc.parse(s):
##    print(atom)



out_file = open("res.xml","w+");
    
for item in var_names:
    str = ("<mGameStrings dwGameStringID=\"%r\" dwGameStringIDName=\"%s\" nErrorCode=\"%r\" szShowPos=\"CMST_MESSAGEBOX;CMST_ANNOUNCEMENT\" szGameString=\"%s\">\n" \
        "\t<szShowPosArray IsStaticArray=\"0\" [XlsProp]=\"0\" ArrayCount=\"2\">\n"\
					"\t\t<szShowPosArray v=\"22\"/>\n" \
					"\t\t<szShowPosArray v=\"20\"/>\n" \
				"\t</szShowPosArray>\n</mGameStrings>\n") % (item.value,item.symbol,item.value,item.comment)
    out_file.write(str)

out_file.close()



        
    ##print(name,var_names[name])
    ##print(var_names[name].value,var_names[name].symbol,var_names[name].comment)
    
     
